<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/28
 * Time: 17:43
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Classification extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['course_type_name'] = ['like',"%" . $keyword . "%"];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('course_type')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('course_type')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('course_type_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }



    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        //查询课程是否大于0
        $num = Db::table('up_course')->where('f_id',$task_id)->count();
        if($num>0){
            Db::table('up_course')->where('f_id',$task_id)->delete();
            $course_id = Db::table('up_course')->where('f_id',$task_id)->column('course_id');
            //统计这个
            $num1 = Db::table('up_course_chapter')->where('course_id','IN',$course_id)->count();
            if($num1>0){
                Db::table('up_course_chapter')->where('course_id','IN',$course_id)->delete();
            }
        }
        $res = Db::name('course_type')->where('course_type_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('course_type')->where('course_type_id',$task_id)->setField('course_type_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('course_type')->where('course_type_id',$task_id)->setField('course_type_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $course_type_name =input('course_type_name');
            $data['course_type_name'] = $course_type_name;
            $data['course_type_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_course_type')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $course_type_name =input('course_type_name');
            $data['course_type_name'] = $course_type_name;
            $res = Db::table('up_course_type')->where('course_type_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('course_type_id');
        $info= Db::table('up_course_type')->where('course_type_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}